package com.hzhjhh.view.loginView;

import java.awt.Frame;

import javax.swing.JDialog;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dialog;

import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.entity.UserInfo;
import com.hzhjhh.init.mainFrame.MainFrame;
import com.hzhjhh.mapper.UserInfoMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.cashierView.VenditionDialog;

import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JOptionPane;

import java.util.Objects;
import java.util.Vector;

import javax.swing.JPasswordField;

/**
 * 管理员登录页面
 * 
 * @author Administrator
 *
 */
public class Admin extends JDialog {
	
	VenditionDialog form;
	JPanel panel1 = new JPanel();
	JTextField txfLoginName;
	JLabel lbLoginName;
	JLabel lbPassword;
	JLabel lbLoginTitle;
	JButton btLogin;
	JButton btCancel;
	JPasswordField txfLoginPs;
	
	private final static String TITLE_TEXT = "管理员登录";
	
	private UserInfoMapper userInfoMapper;

	private Admin(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		try {
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			jbInit();
			// 添加一个对话框的监听器，当直接关闭的时候，回到收银页面
			addWindowListener(new WindowAdapter() {
				
				@Override
				public void windowClosing(WindowEvent e) {
					// 登录页面关闭后，回到原来的前台收银页面
					form.setVisible(true);
					form.setAdminInitFlag(false);
				}
			});
			pack();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		// 初始化完成后，注入数据库操作接口
		SqlSession session = DBUtils.getInstance().getSession(true);
		userInfoMapper = session.getMapper(UserInfoMapper.class);
	}

	public Admin() {
		this(new Frame(), TITLE_TEXT, false);
		this.setSize(400, 300);
		this.setLocation(300, 200);

	}

	public Admin(VenditionDialog form) {
		this(new Frame(), TITLE_TEXT, false);
		this.setSize(400, 300);
		this.setLocation(300, 200);
		this.setVisible(true);
		this.form = form;
	}

	private void jbInit() throws Exception {
		panel1.setLayout(null);
		this.getContentPane().setBackground(Color.pink);
		panel1.setBackground(Color.pink);
		
		getContentPane().add(panel1);
		lbLoginTitle = initLbLoginTitle();
		panel1.add(lbLoginTitle);
		lbLoginName = initLbLoginName();
		panel1.add(lbLoginName);
		txfLoginName = initTxfLoginName();
		panel1.add(txfLoginName);
		lbPassword = initLbPassword();
		panel1.add(lbPassword);
		btLogin = initBtLogin();
		panel1.add(btLogin);
		btCancel = initBtCancel();
		panel1.add(btCancel);
		txfLoginPs = initTxfLoginPs();
		panel1.add(txfLoginPs);
	}
	/**
	 * 初始化登录标题标签
	 */
	private JLabel initLbLoginTitle() {
		JLabel loginTitle = new JLabel();
		loginTitle.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
		loginTitle.setText(TITLE_TEXT);
		loginTitle.setBounds(new Rectangle(145, 30, 113, 27));
		return loginTitle;
	}
	/**
	 * 初始化登录名称标签
	 */
	private JLabel initLbLoginName() {
		JLabel loginName = new JLabel();
		loginName.setText("用户名");
		loginName.setBounds(new Rectangle(78, 87, 49, 25));
		return loginName;
	}
	/**
	 * 初始化登录名称输入框
	 */
	private JTextField initTxfLoginName() {
		JTextField loginName = new JTextField();
		loginName.setBounds(new Rectangle(158, 87, 101, 25));
		loginName.setText("admin");
		return loginName;
	}
	/**
	 * 初始化登录密码输入框
	 */
	private JLabel initLbPassword() {
		JLabel password = new JLabel();
		password.setText("密  码");
		password.setBounds(new Rectangle(76, 124, 49, 25));
		return password;
	}
	/**
	 * 初始化登录按钮
	 */
	private JButton initBtLogin() {
		JButton login = new JButton();
		login.setBounds(new Rectangle(75, 206, 83, 25));
		login.setText("登  录");
		login.addActionListener(event -> {
			Vector user = new Vector();
			String name = txfLoginName.getText();
			String password = txfLoginPs.getText();
			if (StringUtils.isAnyBlank(name, password)) {
				JOptionPane.showConfirmDialog(new Dialog(this), "用户名和密码全部不能为空");
				return;
			}
//			user = Select_Means.Select_User(name, password);
			UserInfo userInfo = userInfoMapper.selectByLoginInfo(name, password, "超级管理员");
			if (Objects.isNull(userInfo)) {
				JOptionPane.showMessageDialog(this, "用户名或密码不正确,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			// 管理员信息存在，则初始化进销存管理页面，并关闭登录对话框
			this.dispose();
			MainFrame mainFrame = new MainFrame();
			mainFrame.setVisible(true);
			mainFrame.setLocation(50, 50);
			form.setVisible(false);
		});
		return login;
	}
	/**
	 * 初始化取消按钮
	 */
	private JButton initBtCancel() {
		JButton jButton = new JButton();
		jButton.setBounds(new Rectangle(210, 206, 83, 25));
		jButton.setText("取  消");
		jButton.addActionListener(event -> {
			this.dispose();
//			this.setVisible(false);
			// 取消登录后，回到收银管理页面
			form.setVisible(true);
			form.setAdminInitFlag(false);

		});
		return jButton;
	}
	/**
	 * 初始化登录密码输入框
	 */
	private JPasswordField initTxfLoginPs() {
		JPasswordField passwordField = new JPasswordField();
		passwordField.setBounds(new Rectangle(158, 125, 101, 25));
		passwordField.setText("admin");
		return passwordField;
	}

}

// Vector user = new Vector();
// String name = jTextField1.getText();
// String password = jTextField2.getText();
// user = Select_Means.Select_User(name, password);
// if (user.size() != 0) {
// MainFrame mainFrame = new MainFrame();
// mainFrame.setVisible(true);
// mainFrame.setLocation(50, 50);
// this.setVisible(false);
// Vendition_dialog vdialog = new Vendition_dialog();
// vdialog.setVisible(false);
// }
//
// else {
// JOptionPane.showMessageDialog(this, "用户名或密码不正确,请重新输入", "错误",
// JOptionPane.ERROR_MESSAGE);
// return;
// }